<template>
	<view class="container">
		<u--form ref="queryForm" :model="query">
			<u-form-item label="" prop="box" style="display: flex;">
				<span style="margin-right: 20rpx;">容器码</span>
				<u--input 
					placeholder="请输入/扫描" 
					v-model="query.containerCode" 
					suffixIcon="scan"
					suffixIconStyle="font-size: 68rpx"
					style="flex: 1;"
					@confirm="show = true"
				>
				</u--input>
			</u-form-item>
		</u--form>
		<u-modal
			:show="show"
			:title="title"
			:content='content'
			:confirmText='confirmText'
			:showCancelButton="true"
			:cancelText='cancelText'
			@cancel="show = false"
			@confirm="changeContainer"></u-modal>
	</view>
</template>

<script>
	import { changeContainerAPI } from '@/api/work/sort'
	export default {
		data() {
			return {
				query: {
					containerCode: ''
				},
				show: false,
				title: '操作提示',
				content: '确认容器已经满箱, 点击确认即换箱,是否继续？',
				confirmText: '确认',
				cancelText: '取消'
			};
		},
		onLoad(options) {
			this.query.containerCode = options.containerCode
			if (this.query.containerCode) {
				this.show = true
			}
			uni.$on('scancodedate',(data) => this.getCode(data.code))
		},
		onUnload() {
		  // 移除监听事件      
		  uni.$off('scancodedate')
		},
		methods: {
			getCode(code) {
				const pattern = /^[A-Za-z]\d{9}$/
				if (pattern.test(code)) {
					this.query.containerCode = code
					if (this.query.containerCode) {
						this.show = true
					}
				}
			},
			async changeContainer() {
				const res = await changeContainerAPI(this.query.containerCode)
				if (res.meta.code === 200) {
					this.show = false
					this.query = {
						containerCode: ''
					}
					return this.$refs.uToast.show({
						type: 'success',
						message: res.meta.msg
					})
				} else {
					this.show = false
					return this.$refs.uToast.show({
						type: 'warning',
						message: res.meta.msg
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>

</style>
